<?php

namespace C\P;

const ERROR_CODE = [
  '30' => '密码错误',
  '40' => '账号不存在',
  '41' => '余额不足',
  '43' => 'IP地址限制',
  '50' => '内容含有敏感词',
  '51' => '手机号码不正确',
];


class SMSBao
{
    private $username;
    private $password;

  /**
   * @param $username
   * @param $password
   */
    public function __construct($username, $password)
    {
        $this->username = $username;
        $this->password = $password;
    }

  /**
   * @param $mobile
   * @param $content
   */
    public function send($mobile, $content)
    {
        $queryParams = [
        'u' => $this->username,
        'p' => md5($this->password),
        'm' => $mobile,
        'c' => $content,
        ];

        $queryString = http_build_query($queryParams);

        $url = 'https://api.smsbao.com/sms?' . $queryString;

        $response = Http::get($url, [], 10);
        if ($response == null) {
            throw new \Exception('请求异常');
        }

        if ($response == '0') {
            return true;
        }

        throw new \Exception(ERROR_CODE[$response]);
    }
}
